import json

# 从文件中解析json数据
def Parse_filedata_tojson(directory):
    """
    ### 从文件中解析json数据 <br>
    - directory : 文件路径 <br>
    """
    with open(directory, 'r', encoding='utf-8') as file:
        # 读取并解析JSON数据
        data = json.load(file)
    return data

# 写入JSON文件
def write_json(file_path, data):
    with open(file_path, 'w', encoding="utf-8") as file:
        json.dump(data, file, ensure_ascii=False, indent=4)


def ReadConfigData(fliepath,savename):
    read_jsondata = Parse_filedata_tojson(fliepath)
    for jsondata_item in read_jsondata["data"]:
        if(jsondata_item["savename"]!=savename):
            continue
        source_ip=jsondata_item["source_ip"]
        source_port=jsondata_item["source_port"]
        source_user=jsondata_item["source_user"]
        source_password=jsondata_item["source_password"]
        source_file=jsondata_item["source_file"]
        filename=jsondata_item["filename"]
        target_ip=jsondata_item["target_ip"]
        target_port=jsondata_item["target_port"]
        target_user=jsondata_item["target_user"]
        target_password=jsondata_item["target_password"]
        target_file=jsondata_item["target_file"]
        return source_ip,source_port,source_user,source_password,source_file,filename,target_ip,target_port,target_user,target_password,target_file
    return "",0,"","","","","",0,"","",""

def ReadconfigList(fliepath):
    configList=()
    read_jsondata = Parse_filedata_tojson(fliepath)
    for jsondata_item in read_jsondata["data"]:
        configList=configList+(jsondata_item["savename"],)
    return configList

def WriteConfigData(fliepath,savename,source_ip,source_port,source_user,source_password,source_file,filename,target_ip,target_port,target_user,target_password,target_file):
    read_jsondata = Parse_filedata_tojson(fliepath)
    for jsondata_item in read_jsondata["data"]:
        if(jsondata_item["savename"]!=savename):
            continue
        jsondata_item["source_ip"]=source_ip
        jsondata_item["source_port"]=source_port
        jsondata_item["source_user"]=source_user
        jsondata_item["source_password"]=source_password
        jsondata_item["source_file"]=source_file
        jsondata_item["filename"]=filename
        jsondata_item["target_ip"]=target_ip
        jsondata_item["target_port"]=target_port
        jsondata_item["target_user"]=target_user
        jsondata_item["target_password"]=target_password
        jsondata_item["target_file"]=target_file
        write_json(fliepath,read_jsondata)
        return True
    
    data_item = {
            "savename":savename,
            "source_ip":source_ip,
            "source_port" : source_port,
            "source_user" : source_user,
            "source_password" : source_password,
            "source_file" :source_file,
            "filename" : filename,
            "target_ip" : target_ip,
            "target_port" : target_port,
            "target_user" : target_user,
            "target_password" : target_password,
            "target_file" :target_file
        }
    read_jsondata["data"].append(data_item)
    write_json(fliepath,read_jsondata)
    return True















if __name__ == '__main__':  
    print("confile_deal")